<template>
    <div>
        <h2>房价增加</h2>
        <table class="table table-bordered">
            <tbody>

                <tr>
                    <td align="right">放假名称</td>
                    <td><input type="text" v-model="info.Name"/></td>
                </tr>

                <tr>
                    <td align="right">房间价格</td>
                    <td><input type="text" v-model="info.Money"/> </td>
                </tr>


                <tr>
                    <td align="right">联系方式</td>
                    <td><input type="text" v-model="info.Phone"/> </td>
                </tr>

                <tr>
                    <td align="right">地址</td>
                    <td><input type="text" v-model="info.Addr"/> </td>
                </tr>

                <tr>
                    <td align="right">图片</td>
                    <td><input type="file" @change="img"/> 
                        <img :src="info.Photo" width="100" height="100" v-if="info.Photo != ''"/>
                    </td>
                </tr>

                <tr>
                    <td align="right">状态</td>
                    <td>
                        <input type="radio" value="1" v-model="info.Stare"/>可用
                        <input type="radio" value="0" v-model="info.Stare"/>禁用
                    </td>
                </tr>


                <tr>
                    <td align="right"></td>
                    <td>
                        <input type="button" value="保存" class="btn btn-info" @click="ok"/>
                    </td>
                </tr>


            </tbody>
        </table>
    </div>
</template>

<script setup lang="ts">
import {ref,reactive} from 'vue'
import axios from 'axios';
import { useRouter,useRoute } from 'vue-router';

let router = useRouter();
let route = useRoute();


let info:any = reactive({
    Name:'',
  Money: '',
  Phone: '',
  Addr: '',
  Photo: '',
  Stare: 1,
})


const img = (e:any)=>{
    let obj = e.target.files[0];
    let f = new FormData();
    f.append('file',obj)
    axios({
        url:'http://localhost:5214/api/Img/Imgs',
        method:'post',
        data:f
    })
    .then((res)=>{
        if(res.data == '图片大小不合适')
        {
            alert('图片格式或大小不正确')
            return;
        }
        else
        {
            info.Photo = res.data;
        }
    })
    .catch((err)=>{
        console.log(err);
    })
}


const ok = ()=>{
    if(info.Name == "")
    {
        alert('房价不能为空');
        return;
    }

    axios({
        url:'http://localhost:5214/api/House/AddHouse',
        method:'post',
        data:info
    })
    .then((res)=>{
        if(res.data > 0)
        {
            alert('保存成功');

        }
        else
        {
            alert('保存失败');
        }
    })
    .catch((err)=>{
        console.log(err);
    })







}








</script>

<style scoped>

</style>